package com.intelligent.controller;

import com.intelligent.pojo.Workshops;
import com.intelligent.service.WorkshopsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/workshops")
public class WorkshopsController {

    @Autowired
    private WorkshopsService workshopsService;

    /**
     * 分页查询车间信息（关联用户表）
     * @param current 当前页
     * @param size 每页大小
     * @param workshopCode 车间编号（可选）
     * @param workshopName 车间名称（可选）
     * @param username 负责人用户名（可选）
     * @param status 状态（可选）
     * @return 分页结果
     */
    @GetMapping("/page")
    public IPage<Workshops> getWorkshopsPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String workshopCode,
            @RequestParam(required = false) String workshopName,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer status) {
        
        Page<Workshops> page = new Page<>(current, size);
        return workshopsService.getWorkshopsPage(page, workshopCode, workshopName, username, status);
    }

    /**
     * 根据ID查询车间信息（关联用户表）
     * @param id 车间ID
     * @return 车间信息
     */
    @GetMapping("/{id}")
    public Workshops getWorkshopsById(@PathVariable Long id) {
        return workshopsService.getWorkshopsById(id);
    }

    /**
     * 添加车间信息（自动生成车间编号）
     * @param workshops 车间信息
     * @return 是否添加成功
     */
    @PostMapping
    public boolean addWorkshop(@RequestBody Workshops workshops) {
        return workshopsService.addWorkshop(workshops);
    }

    /**
     * 生成车间编号
     * @return 生成的车间编号
     */
    @GetMapping("/generate-code")
    public String generateWorkshopCode() {
        return workshopsService.generateWorkshopCode();
    }

    /**
     * 更新车间信息
     * @param workshops 车间信息
     * @return 是否更新成功
     */
    @PutMapping
    public boolean updateWorkshop(@RequestBody Workshops workshops) {
        return workshopsService.updateWorkshop(workshops);
    }

    /**
     * 根据ID删除车间信息
     * @param id 车间ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean deleteWorkshopById(@PathVariable Long id) {
        return workshopsService.deleteWorkshopById(id);
    }
}
